package yao.spider.jsoup.common;

import java.net.UnknownHostException;
import java.util.ArrayList;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

public class MongoUtils {
	
	public static DBCollection getCollection(String documentName) {
		ServerAddress address;
		try {
			address = new ServerAddress("112.124.98.245", 27017);
		} catch (UnknownHostException e) {			
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		MongoClientOptions options = MongoClientOptions.builder()
				.connectTimeout(15000)
				.connectionsPerHost(100)
				.threadsAllowedToBlockForConnectionMultiplier(4)
				.socketKeepAlive(true)
				.socketTimeout(9000).build();		
		
		final MongoCredential credential = MongoCredential.createScramSha1Credential("root", "admin", "n7RyM#QCvPvCQrD2d6".toCharArray());
		Mongo mongo = new MongoClient(address,
			new ArrayList<MongoCredential>(){
				private static final long serialVersionUID = 1L;{
					add(credential);
				}
			},options);		
		//库名
		DB db = mongo.getDB("zhinengbao");		
		//文档
		DBCollection collection = db.getCollection(documentName);
		return collection;		
		//插入
	/*	DBObject dbObject = new BasicDBObject("title", "测试标题").append("desc", "测试描述").append("isOne", false);
		collection.insert(dbObject);*/
	}
	
	

	
}